﻿using System.Collections.Generic;
using System.Linq;
using NPOI.SS.Formula.Functions;
using Ornament.ImportHelper;

namespace Fee.IO.Importers
{
    public class OrderContractImporter<T> : BaseImporter<T> where T : FeeDbContext
    {
        public OrderContractImporter(T context, bool saveInDb) : base(context, saveInDb)
        {
        }

        protected override bool ConverTo(IList<string> rowStringInfo, out ImportResultItem item)
        {
            //订单编号	合同号	订单名称	供应商	税率	联系人	合同含税金额（元）	合同不含税金额（元）	合同签订日期	合同状态	框架合同号	投资项目编码	备注

            item = new ImportResultItem();
            var columnIndex = 0;
            var no = rowStringInfo.ImportString(columnIndex, "订单编号", item);


            var result = Context.OrderContracts
                             .FirstOrDefault(_ => _.OrderNo == no) ?? new OrderContract()
                             {
                                 OrderNo = no,
                             };
            columnIndex++;
            result.ContractNo = rowStringInfo.ImportString(columnIndex, "合同号", item, false);
            columnIndex++;
            result.Name = rowStringInfo.ImportString(columnIndex, "订单名称", item);
            columnIndex++;
            result.Vendor = rowStringInfo.ImportVendorByFullName(columnIndex, "供应商", item, Context);
            columnIndex++;
            result.Tax = rowStringInfo.ImportPrecent(columnIndex, "税率", item, true);
            columnIndex++;
            result.Contact = rowStringInfo.ImportString(columnIndex, "联系人", item);
            columnIndex++;

            result.Amount = rowStringInfo.ImportDecimal(columnIndex, "合同含税金额", item, false);
            columnIndex++;
            result.AmountWithoutTax = rowStringInfo.ImportDecimal(columnIndex, "合同不含税金额", item, false);
            columnIndex++;
            result.SignTime = rowStringInfo.ImportDatetime(columnIndex, "合同签订日期", item, false);
            columnIndex++;

            result.Status = rowStringInfo.ImportEnum<ContractStatus>(columnIndex, "合同状态", item, true);
            columnIndex++;

            result.FrameworkContract = rowStringInfo.ImportFrameworkContract(columnIndex, "框架合同号", item, Context);
            columnIndex++;

            result.Project = rowStringInfo.ImportProject(columnIndex, "投资项目编码", item, Context);
            result.ProjectNo = rowStringInfo.ImportString(columnIndex, "投资项目编码", item, true);
            columnIndex++;

            result.Comment = rowStringInfo.ImportString(columnIndex, "备注", item, false);

            if (!item.HasError)
            {
                item.Data.Add(result);
                return true;
            }

            return false;
        }
    }
}